import {
	pcaa
} from 'area-data-vue';
class addressListData {
	// 构造函数
	constructor() {
		this.sourceData = pcaa // 所有地址源数据
		this.data = [] // 组合数据，即选择器使用的数据
		this.defaultValue = []
	}
	/**
	 * @param {Object} arry 设置默认值
	 */
	setDefaultValue(arry = []){
		if(arry && arry.length > 0){
			this.defaultValue = arry
		}
	}
	/**
	 * 添加省数据
	 */
	addProvinceData(e) {
		const that = this
		that.data = []
		// 把对象转换成数组
		// 拿到数据的key用作value
		const pValue = Object.keys(that.sourceData['86'])
		// 拿到数据的value用作label
		const pLabel = Object.values(that.sourceData['86'])
		// 遍历数组长度
		pValue.forEach((v, i) => {
			// 组合数据
			let province = {
				value: pValue[i],
				label: pLabel[i],
				children: []
			}
			// 为最外层数组添加数据
			that.data.push(province)
		})
		if(that.defaultValue.length > 0){
			that.data.filter(item=>{
				if(item.value == that.defaultValue[0]){
					that.addCityData(item,[that.defaultValue[0]])
					if(e == 1){
						that.pushDefault(item.children,1)
					}else{
						that.pushDefault(item.children)
					}
					
				}
			})
		}
		
	}
	/**
	 * 添加默认数据
	 */
	pushDefault(item,e){
		const that = this
		if(that.defaultValue[1]){
			item.filter(item=>{
				if(item.value == that.defaultValue[1]){
					if(e != 1){
						that.addCityData(item,[that.defaultValue[0],that.defaultValue[1]])
					}else{
						delete item.children
					}
					if(that.defaultValue[2]){
						that.threeLevel(item.children)
					}
				}
			})
		}
		
	}
	threeLevel(item){
		const that = this
		if(that.defaultValue[2]){
			item.filter(item=>{
				if(item.value == that.defaultValue[2]){
					that.addCityData(item,[that.defaultValue[0],that.defaultValue[1],that.defaultValue[2]])
				}
			})
		}
	}
	/**
	 * 加载当前点击选项的数据
	 */
	addCityData(item, e) {
		return new Promise((resolve) => {
				const that = this
				const cValue = Object.keys(that.sourceData[item.value])
				// 拿到数据的value用作label
				const cLabel = Object.values(that.sourceData[item.value])
				cValue.forEach((c, n) => {
					let city = {}
						city.value = cValue[n]
						city.label = cLabel[n]
						if(e.length < 3){
							city.children = []
						}
						// console.log(e)
						// console.log(city)
					item.children.push(city)
				})
				resolve(item)
			
		})

	}
}
export default addressListData
